package com.herench.bs.handler;

import com.herench.bs.agent.IPersonAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.utils.IdCardUtils;
import com.herench.bs.utils.NameUtils;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.MD5Util;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人注册（包含手机号、个人信息）
 * Created by ThinkPad on 2017/8/25.
 */
@Component
@Handler(code="100201",name="个人注册")
public class PersonRegisterHandler extends AbstractMessageHandler {


    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        Map<String, Object> res = new HashMap<>();

        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        String phone = requestMessage.getArgsPropertyStringValue("phone");
        String verificationCode = requestMessage.getArgsPropertyStringValue("verifyCode");
        String password = requestMessage.getArgsPropertyStringValue("password");
        String userType = requestMessage.getArgsPropertyStringValue("userType");
        String user = requestMessage.getArgsPropertyStringValue("user");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        String name = requestMessage.getArgsPropertyStringValue("name");
        String registerChannel = requestMessage.getArgsPropertyStringValue("fromProduct");
        String registerProtocol = requestMessage.getArgsPropertyStringValue("registerProtocol");
        String appImei = requestMessage.getArgsPropertyStringValue("appImei");
        String webchat = requestMessage.getArgsPropertyStringValue("webchat");

        Assert.isTrue(StringUtils.hasLength(name), "name 不能为空.");
        Assert.isTrue(StringUtils.hasLength(phone), "phone 不能为空.");
        Assert.isTrue(StringUtils.hasLength(idCard), "idCard 不能为空.");
        Assert.isTrue(StringUtils.hasLength(password), "password 不能为空.");
        Assert.isTrue(StringUtils.hasLength(verificationCode), "verifyCode 不能为空.");
        Assert.isTrue(StringUtils.hasLength(channelCode), "sysCode 不能为空.");
        Assert.isTrue(StringUtils.hasLength(registerChannel), "fromProduct 不能为空.");

        responseMessage = new ResponseMessage(requestMessage.getId());
        if(IdCardUtils.isIDNumber(idCard)&&NameUtils.ChineseName(name)) {
            IPersonAgent personAgent = this.getCleverClinicContext().getApplicationContext().getBean(IPersonAgent.class);

            {
                res = personAgent.personRegister(name, phone, idCard, password, verificationCode, channelCode, registerChannel, appImei, webchat);
                responseMessage.setDataAttribute("fromProduct", registerChannel);
                responseMessage.setDataAttribute("idCard", idCard);
                responseMessage.setDataAttribute("name", name);
                responseMessage.setDataAttribute("password", MD5Util.string2MD5(MD5Util.string2MD5(password)));
                responseMessage.setDataAttribute("phone", phone);
                responseMessage.setDataAttribute("registerProtocol", registerProtocol);
                responseMessage.setDataAttribute("sysCode", channelCode);
                responseMessage.setDataAttribute("user", idCard);
                responseMessage.setDataAttribute("userType", userType);
            }
            {
                String mobile = requestMessage.getArgsPropertyStringValue("phone");
                String defaultFlag = "1";
                String verifyPhone = "0";
                String user1 = requestMessage.getArgsPropertyStringValue("phone");
                Map<String, Object> resPerson = personAgent.addRelatedUserId(user1, name, idCard, channelCode, mobile, verificationCode, defaultFlag, verifyPhone);
                if (res.get("code").equals("0")) {
                    res.put("name", name);
                    res.put("idCard", idCard);
                    res.put("mobile", mobile);
                    res.put("user", user);
                }
            }
        }
        else{
            res.put(ResponseMessage.RES_CODE, "-1");
            res.put(ResponseMessage.MESSAGE, "请核对身份证号和姓名后再输入");
        }
        getPropertiesFromInvokerResultMap(responseMessage, res);

        return responseMessage;

    }
}
